package com.liuweiheng.cloudproviderpayment8002.mq.direct;

import com.liuweiheng.cloudproviderpayment8002.mq.MultiConstant;
import com.liuweiheng.cloudproviderpayment8002.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {

    public static void main(String[] args) throws IOException {

        Connection connection = RabbitMQUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(Provider.EXCHANGE_NAME, MultiConstant.DIRECT_EXCHANGE_TYPE, true);

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, Provider.EXCHANGE_NAME, Provider.ERROR);

        Channel finalChannel = channel;

        channel.basicConsume(queueName, false, new DefaultConsumer(finalChannel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2：" + new String(body));
                finalChannel.basicAck(envelope.getDeliveryTag(), false);
            }
        });

    }

}
